﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UCenter.Core.Repository;
using UCenter.Models;
using UCenter.DataProvider;

namespace UCenter.SQLServerProvider
{
    class ModuleRepository : RepositoryBase, IModuleRepository
    {
        public List<Module> GetModules(int userId)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                return (from u in db.GetTable<User>()
                        from m in db.GetTable<Module>()
                        from userModule in db.GetTable<UserModuleMap>()
                        where u.ID == userModule.UserId && userModule.ModuleId == m.ID && u.ID == userId
                        orderby m.ID ascending
                        select m).ToList<Module>();

            }
        }
                        
        public Module GetModuleById(int Id)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                return db.GetTable<Module>().SingleOrDefault(m => m.ID == Id);
            }
        }
    }
}
